package com.zff.controller;


import com.zff.dao.XstWxUserDao;
import com.zff.dao.YsjGaojainDao;
import com.zff.entity.XstWxUser;
import com.zff.entity.YsjGaojain;
import com.zff.tool.QiniuUtil;
import com.zff.tool.RandomString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zff
 * @since 2020-08-27
 */
@RestController
@RequestMapping("/upload")
public class YsjGaojainController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private YsjGaojainDao ysjGaojainDao;

    @Autowired
    private XstWxUserDao xstWxUserDao;

    @PostMapping("/uploadVideo1")
    public String upload1(MultipartFile file) {
        System.out.println("file:"+file);
        return "success";
    }

    /**
     * 论文投稿
     * @param file
     * @param req
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadVideo")
    public String upload(MultipartFile file, HttpServletRequest req) throws IOException {
        String userId=req.getParameter("userId");
        System.out.println("userId:"+userId);
        String domainOfBucket = "ywbs1.fxiang.cn";
        XstWxUser wxUser = xstWxUserDao.selectById(userId);

        String filen =file.getOriginalFilename();
        String studySection=filen.substring(0, filen.indexOf("."));
        String fileType=filen.substring(studySection.length()+1, filen.length());
        String fileName1 = null;
        YsjGaojain ysjGaojain = new YsjGaojain();
        int aaa = (int)((Math.random()*9+1)*1000);
        fileName1 ="xspx/xspx--"+userId+"-"+aaa + "."+fileType;
        String fileName2 = "xspx--"+userId+"-"+aaa + "."+fileType;
        ysjGaojain.setType("1");
        String fileName = fileName1;
        String filePath = domainOfBucket+"/"+fileName;
        /*if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }*/
        String res = QiniuUtil.testUpload(file.getBytes(), file, userId, fileName1,fileName2);
        int userid = Integer.parseInt(userId);
        ysjGaojain.setUserId(userid);
        ysjGaojain.setFilename(file.getOriginalFilename());
        ysjGaojain.setFilePath(filePath);
        ysjGaojainDao.insert(ysjGaojain);
        HashMap<String, Object> map = new HashMap<>();
        map.put("filePath",filePath);
        List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map);
        System.out.println("res:"+res);
        return res;
    }

    @PostMapping("/uploadVideo2")
    public List<YsjGaojain> upload2(@RequestParam("file") MultipartFile file,@RequestParam("file2") MultipartFile file2, HttpServletRequest req) throws IOException {
        String userId=req.getParameter("userId");
        System.out.println("userId:"+userId);
        String domainOfBucket = "ywbs1.fxiang.cn";
        XstWxUser wxUser = xstWxUserDao.selectById(userId);

        String filen =file.getOriginalFilename();
        String filen2 =file2.getOriginalFilename();
        String studySection=filen.substring(0, filen.indexOf("."));
        String fileType=filen.substring(studySection.length()+1, filen.length());
        String fileName1 = null;
        YsjGaojain ysjGaojain = new YsjGaojain();
        int aaa = (int)((Math.random()*9+1)*1000);
        fileName1 ="xspx--"+userId+"-"+aaa + "."+fileType;
        String fileName2 = "xspx--"+userId+"-"+aaa + "."+fileType;
        ysjGaojain.setType("1");
        String fileName = fileName1;
        String filePath = domainOfBucket+"/"+fileName;
        /*if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }*/
        QiniuUtil.testUpload(file.getBytes(),file,userId,fileName1,fileName2);
        int userid = Integer.parseInt(userId);
        ysjGaojain.setUserId(userid);
        ysjGaojain.setFilename(file.getOriginalFilename());
        ysjGaojain.setFilePath(filePath);
        ysjGaojainDao.insert(ysjGaojain);
        HashMap<String, Object> map = new HashMap<>();
        map.put("filePath",filePath);
        List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map);
        return ysjGaojains;
    }


    @GetMapping("/getFileInfo")
    public List<YsjGaojain> getFileInfo(String userId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        List<YsjGaojain> ysjGaojains = ysjGaojainDao.selectByMap(map);
        return ysjGaojains;
    }

    @PostMapping("/fileupload")
    public Map<String,Object> fileupload(MultipartFile file, HttpServletRequest req){
        String userId=req.getParameter("userId");
        System.out.println("userId:"+userId);
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd");
        Map<String,Object> result = new HashMap<>();
        String originalFilename = file.getOriginalFilename();//上传文件名
        if(!originalFilename.endsWith(".docx")){
            result.put("status","error");
            result.put("msg","文件类型不对");
            return result;
        }
        String format = sdf.format(new Date());
        //String realPath = req.getServletContext().getRealPath("/") + format;//项目路径
        String realPath = "/" + format;//项目路径
        System.out.println("上传文件路径realPath:"+realPath);
        File folder = new File(realPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        //String newName = UUID.randomUUID().toString() + ".docx";
        String newName =UUID.randomUUID().toString() + originalFilename;
        System.out.println("newName:"+newName);
        try {
            file.transferTo(new File(folder,newName));
            String url = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
            System.out.println("url:"+url);
            result.put("status","success");
            result.put("url",url);
        } catch (IOException e) {
            result.put("status","error");
            result.put("msg",e.getMessage());
        }
        return result;
    }

}

